package joystickjava;

import de.hardcode.jxinput.Axis;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import de.hardcode.jxinput.Button;
import de.hardcode.jxinput.JXInputDevice;
import de.hardcode.jxinput.JXInputManager;
import de.hardcode.jxinput.virtual.JXVirtualInputDevice;


public class TryJoystick implements ActionListener {

			private JXVirtualInputDevice mVirtualDevice = null;
			Timer t = new Timer(50, this);
			static String joystickName = "Logitech Extreme 3D";

            
	      	//The number cast is a clock
	      	//This number is returned to OPS

	      	//Direction
	      	public static int FORWARD = 0;
	      	public static int UP = 1;
	      	public static int DOWN = 2;
	      	public static int LEFT = 3;
            public static int RIGHT = 4;
            public static int NO_DIRECTION = 10;


	      	//you must look only to Y;
	      	//don't consider the speed if the direction is only RIGHT or LEFT

	      	public static int speed = 10;


	      	JXInputDevice joystick;
	      	private Axis xAxis;
	      	private Axis yAxis;
            //Axis zAxis;

	      	private Button riseButton;
	      	private Button lowerButton;
            
            //OPS
            OPSInterface opsInt;

	    
	      	public TryJoystick()
	        	{
                    opsInt = new OPSInterface();
                    opsInt.initOPS();
	                initComponents();
                    t.start();
	            }

	      	private void initComponents() {

	      		int numberOfDevices = JXInputManager.getNumberOfDevices();

	      //		for (int i = 0;i <= numberOfDevices; i++){
	        //        if (JXInputManager.getJXInputDevice(0).getName() == joystickName) {
	                	joystick = JXInputManager.getJXInputDevice(0);
                        
	                	int numberOfAxis = joystick.getNumberOfAxes();
	                	int numberOfButtons = joystick.getNumberOfButtons();


	                	//Axis position X
		                xAxis = joystick.getAxis(de.hardcode.jxinput.Axis.ID_X);
                        
		                //Axis position Y
		                yAxis = joystick.getAxis(de.hardcode.jxinput.Axis.ID_Y);
		                //Axis position Z
		                //zAxis = joystick.getAxis(de.hardcode.jxinput.Axis.ID_Z);


		                riseButton = joystick.getButton(2);
		                lowerButton = joystick.getButton(3);
			}

				/**
	             * Implement ActionListener#actionPerformed().
	             * This is called by the Timer.
	            */
	            public void actionPerformed( ActionEvent e )
	        	 {
	                JXInputManager.updateFeatures();


	                //is not a considerable movement if
	                // -0.2 < xAxis/yAxis < +0.2


                    //UP
	                if(riseButton.getState()){
                        int dir = UP;
	                	//System.out.println("Joystick UP, state " + riseButton.getState());
                        opsInt.writeInfoOPS(dir);
	                }

                    //DOWN
					if(lowerButton.getState()){
                        int dir = DOWN;
						//System.out.println("Joystick DOWN, state " + lowerButton.getState());
                        opsInt.writeInfoOPS(dir);
	                }

                   //LEFT
	               if(xAxis.getValue() < - 0.4 ) {
                       int dir = LEFT;
	            	   //System.out.println("Joystick LEFT, value " + xAxis.getValue() );
                       opsInt.writeInfoOPS(dir);
	               }

	               //FORWARD
                    else if(yAxis.getValue() < -0.4 && xAxis.getValue() > -0.7 && xAxis.getValue() < 0.7 ) {
                       int dir = FORWARD;
	            	   //System.out.println("Joystick FORWARD, value " + yAxis.getValue() );
                       opsInt.writeInfoOPS(dir);
	               }

	               //RIGHT
                    else if( xAxis.getValue() > 0.4) {
                       int dir = RIGHT;
	            	   //System.out.println("Joystick RIGHT, value " + xAxis.getValue() );
                       opsInt.writeInfoOPS(dir);
	               }

                   //NO DIRECTION (no keys/buttons pressed)
                   else {
                       int dir = NO_DIRECTION;
                       // System.out.println("Joystick NO_DIRECTION");
                       opsInt.writeInfoOPS(dir);
                    }


	        	 }
	
}
